print('=' * 20)
print("欢迎使用生词本")
print("1.添加新单词")
print("2.删除单词")
print("3.查看单词本")
print("4.清空单词本")
print("5.背单词")
print("6.退出单词本")
print("=" * 20)

import random

word_book=[]

while True:
    n = int(input("请输入您要执行的指令:"))
    per_dict={}

    if  n == 1:#增加
        word=input("请输入要添加的单词:")

        for i in word_book:
            if i.get('单词') == word:
                print('此单词已存在')
                break
        else:
            Translation=input('请输入该词的翻译:')
            if Translation.strip() == ' ':
                print('您的输入有误,请重新输入')
                continue
            else:
                per_dict.update({'单词': word,
                                '翻译':Translation,})
                word_book.append(per_dict)
                print('单词添加成功！') 
                continue  



    elif n == 2:#删除
        if len(word_book) == 0:
            print('生词本内容为空')
        else :
            print('--*' * 6)
            for i in word_book:
                for title, info in i.items():
                    print(title + ':' + info)
                print('--*' * 6)

            word=str(input("请输入您要删除的单词:"))
            for i in word_book:
                if word in i.values():
                    word_book.remove(i)
                    print('删除成功！')
                    break
                else:
                    print("删除的单词不存在~")
                    break



    elif n == 3:#查看
        if len(word_book) == 0:
            print('生词本内容为空~')
        else :
            print('--*' * 6)
            for i in word_book:
                for title, info in i.items():
                    print(title + ':' + info)
                print('--*' * 6)



    elif n == 4:#清空单词本
        if len(word_book) == 0:
            print('生词本内容为空~')
        else :
            word_book.clear()
            print('生词本已清空~')

    elif n == 5:#背单词
        if len(word_book) == 0:
            print('生词本内容为空~')
            break

        while True:
    
            b=random.randint(0,len(word_book))

            print(word_book[b].get('单词'))
            Translation=input('请输入该单词的翻译:')
            if Translation == str(word_book[b].get('翻译')):
                print('太棒了!')
                break
            else:
                print('再想想~')
                continue

    elif n == 6:#结束
        print('您已退出 拜拜！')
        break

    else:
        print('您输入的指令错误,请重新输入:')
        continue









